import type { AxiosInstance } from 'axios';
import axios from 'axios';
import { ElLoading } from 'element-plus';
import Cookies from "js-cookie";

let loadingInstance = null 
// 创建axios的实例
const instacne: AxiosInstance = axios.create({
    // baseURL: 'http://kumanxuan1.f3322.net:8360/admin/',
    baseURL: "http://120.24.64.5:8088/mall-admin/",
    timeout: 30000
})
// 拦截器
//请求拦截
instacne.interceptors.request.use(
    (config) => {
        // 在发送请求之前做什么

        // 开始加载动画
        loadingInstance && loadingInstance.close()
        loadingInstance = ElLoading.service()
        if (Cookies.get("token")) {
            config.headers.Authorization = Cookies.get("token") || "";
            // console.log(config);
        }
        return config;
    },
    (err) => {
        loadingInstance && loadingInstance.close()
        return Promise.reject(err);
    }
)

// 响应拦截
instacne.interceptors.response.use(
    (res) => {
        // 对响应的数据做处理
        loadingInstance && loadingInstance.close()
        if(res.data.code===200){
            return res.data.data
        }else{
            return Promise.reject(res.data.message||"网络异常")
        }
    },
    (err) => {
        loadingInstance && loadingInstance.close()
        return Promise.reject(err);
    }
)

export default instacne;